﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class QuanlyNhanVien : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            timKiemBN("");
        }
        if (!(Session["accountType"].ToString() == "admin"))
        {
            btnThem.Visible = btnXong.Visible = btnXoa.Visible = false;
        }

    }
    void timKiemBN(string chuoi)
    {
        String lstPatientId = "";
        ConnectData cnData = new ConnectData();
        SqlDataReader result = cnData.dataReader("select * from tblPatient where FirstName like '%" + chuoi +
            "%' or LastName  like '%" + chuoi + "%' or Tel like '%" + chuoi + "%'");
        DataTable tb = new DataTable();
        tb.Columns.Add("Họ và tên", typeof(string));
        tb.Columns.Add("Địa chỉ", typeof(string));
        tb.Columns.Add("Ngày sinh", typeof(string));
        tb.Columns.Add("Giới tính", typeof(string));
        tb.Columns.Add("Ngày nhập viện", typeof(string));
        tb.Columns.Add("Số điện thoại", typeof(string));
        tb.Columns.Add("Tên người thân", typeof(string));
        tb.Columns.Add("SĐT người thân", typeof(string));
        int i = 1;
        while (result.Read())
        {
            DataRow row = tb.NewRow();
            lstPatientId += result["PatientId"] + ",";
            row[0] = result["FirstName"] + " " + result["LastName"];
            row[1] = result["Address"];
            row[2] = ((DateTime)result["DateOfBirth"]).ToShortDateString();
            row[3] = (result["Sex"].ToString()=="0")?"Nữ":"Nam";
            row[4] = ((DateTime)result["DateRegistered"]).ToShortDateString();
            row[5] = result["Tel"];
            row[6] = result["NOKFullName"];
            row[7] = result["NOKTel"];
            tb.Rows.Add(row);
        }
        Session["lstPatientId"] = lstPatientId;
        cnData.closeConnect();
        grdDanhSachBN.DataSource = tb;
        grdDanhSachBN.DataBind();
        //visibleContent(employList);
    }
    protected void btnTimKiemBN_Click(object sender, EventArgs e)
    {
        timKiemBN(txtTimKiemBN.Text);
    }
    protected void selectedEmploy(object sender, EventArgs e)
    {
        txtTimKiemBN.Text = sender.ToString();
    }
    protected void grdDanhSachBN_SelectedIndexChanged(Object sender, EventArgs e)
    {
        try
        {
            Session["selectedPid"] = Session["lstPatientId"].ToString().Split(',')[((GridView)sender).SelectedRow.RowIndex];
            Session["status"] = "xem";
            thongtinChiTiet();
        }
        catch { }
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        String[] lstPatientId = Session["lstPatientId"].ToString().Split(',');
        foreach (GridViewRow row in grdDanhSachBN.Rows)
        {
            CheckBox chkRow = (CheckBox)(row.Cells[0].FindControl("chkRow"));
            if (chkRow != null && chkRow.Checked)
            {
                XoaBenhNhan(lstPatientId[row.RowIndex]);
            }
        }
        timKiemBN(txtTimKiemBN.Text);
    }
    protected void XoaBenhNhan(string ID)
    {
        try
        {
            String query = "Delete from tblPatient where PatientId = ";
            ConnectData cnData = new ConnectData();
            SqlDataReader result = cnData.dataReader(query + ID);
        }
        catch { }
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        Session["status"] = "them";
        thongtinChiTiet();
    }
    protected void thongtinChiTiet()
    {
        pnlDanhSachBN.Visible = false;
        pnlThongTinBN.Visible = true;
        if(Session["status"] == "xem")layThongTin(Session["selectedPid"].ToString());
    }
    protected void layThongTin(string pId)
    {
        ConnectData cnData = new ConnectData();
        SqlDataReader result = cnData.dataReader("select * from tblPatient where PatientId = " + pId);
        result.Read();
        txtMaBN.Text = "P" + pId;
        txtFirstName.Text = result["FirstName"] + "";
        txtLastName.Text = result["LastName"] + "";
        txtAddress.Text = result["Address"] + "";
        cmbSex.SelectedValue = result["Sex"] + "";
        txtTel.Text = result["Tel"] + "";
        txtDOB.Text = result["DateOfBirth"] + "";
        txtMarital.Text = result["MarialStatus"] + "";
        txtDateR.Text = result["DateRegistered"] + "";
        txtNOKFullName.Text = result["NOKFullName"] + "";
        txtNOKRelationship.Text = result["NOKRelationship"] + "";
        txtNOKAdd.Text = result["NOKAddress"] + "";
        txtNOKTel.Text = result["NOKTel"] + "";
        txtLDName.Text = result["LDoctorName"] + "";
        txtLDClinic.Text = result["LDoctorClinic"] + "";
        txtLDAdd.Text = result["LDoctorAddress"] + "";
        txtLDTel.Text = result["LDoctorTel"] + "";
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsoluteUri);
    }
    protected void btnXong_Click(object sender, EventArgs e)
    {
        if (Session["status"] == "them")
        {
            themBN(txtFirstName.Text, txtLastName.Text, txtAddress.Text,
                cmbSex.SelectedValue, txtTel.Text, txtDOB.Text, txtMarital.Text,
                txtDateR.Text, txtNOKFullName.Text, txtNOKRelationship.Text,
                txtNOKAdd.Text, txtNOKTel.Text, txtLDName.Text, txtLDClinic.Text,
                txtLDAdd.Text, txtLDTel.Text);
        }
        if (Session["status"] == "xem")
        {
            suaBN(txtFirstName.Text, txtLastName.Text, txtAddress.Text,
                cmbSex.SelectedValue, txtTel.Text, txtDOB.Text, txtMarital.Text,
                txtDateR.Text, txtNOKFullName.Text, txtNOKRelationship.Text,
                txtNOKAdd.Text, txtNOKTel.Text, txtLDName.Text, txtLDClinic.Text,
                txtLDAdd.Text, txtLDTel.Text);
        }
        Response.Redirect(Request.Url.AbsoluteUri);
    }

    private void suaBN(String FirstName, String LastName, String Address, String Sex, String Tel, String DateOfBirth, String MarialStatus, String DateRegistered, String NOKFullName, String NOKRelationship, String NOKAddress, String NOKTel, String LDoctorName, String LDoctorClinic, String LDoctorAddress, String LDoctorTel)
    {
        try
        {
            String query = @"UPDATE [dbo].[tblPatient] SET [FirstName] = N'"
                + FirstName + "', [LastName] = N'" + LastName + "', [Address] = N'" + Address
                + "', [Sex] = N'" + Sex + "', [Tel] = N'" + Tel + "', [DateOfBirth] = N'" + DateOfBirth
                + "', [MarialStatus] = N'" + MarialStatus + "', [DateRegistered] = N'" + DateRegistered
                + "', [NOKFullName] = N'" + NOKFullName + "', [NOKRelationship] = N'" + NOKRelationship
                + "', [NOKAddress] = N'" + NOKAddress + "', [NOKTel] = N'" + NOKTel
                + "', [LDoctorName] = N'" + LDoctorName + "', [LDoctorClinic] = N'" + LDoctorClinic
                + "', [LDoctorAddress] = N'" + LDoctorAddress + "', [LDoctorTel] = N'" + LDoctorTel
                + "' WHERE PatientId = " + txtMaBN.Text.Substring(1);
            ConnectData cnData = new ConnectData();
            SqlDataReader result = cnData.dataReader(query);
        }
        catch { }
    }
    protected void themBN(String FirstName, String LastName, String Address, String Sex, String Tel, String DateOfBirth, String MarialStatus, String DateRegistered, String NOKFullName, String NOKRelationship, String NOKAddress, String NOKTel, String LDoctorName, String LDoctorClinic, String LDoctorAddress, String LDoctorTel)
    {
        try
        {
            String query = @"INSERT INTO [dbo].[tblPatient] ([FirstName] ,[LastName] ,[Address] ,[Sex] ,[Tel] ,[DateOfBirth] ,[MarialStatus] ,[DateRegistered] ,[NOKFullName] ,[NOKRelationship] ,[NOKAddress] ,[NOKTel] ,[LDoctorName] ,[LDoctorClinic] ,[LDoctorAddress] ,[LDoctorTel] )
                            VALUES (N'" + FirstName + "', N'" + LastName + "', N'" + Address + "', N'" + Sex + "', N'" + Tel + "', N'" + DateOfBirth + "', N'" + MarialStatus + "', N'" + DateRegistered + "', N'" + NOKFullName + "', N'" + NOKRelationship + "', N'" + NOKAddress + "', N'" + NOKTel + "', N'" + LDoctorName + "', N'" + LDoctorClinic + "', N'" + LDoctorAddress + "', N'" + LDoctorTel + "')";
            ConnectData cnData = new ConnectData();
            SqlDataReader result = cnData.dataReader(query);
        }
        catch { }
    }
    protected void grdDanhSachBN_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(!(Session["accountType"].ToString()=="admin"))e.Row.Cells[0].Style["display"] = "none";
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sender as GridView, "Select$" + e.Row.RowIndex);
        }
    }
}